<?php
require_once 'config/database.php';
require_once 'controllers/AuthController.php';
require_once 'controllers/PostController.php';
require_once 'controllers/TopicController.php';
require_once 'controllers/CategoryController.php';
require_once 'controllers/UserPostController.php';

header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Methods: POST, OPTIONS");
// header("Access-Control-Allow-Headers: Content-Type, Authorization");
// header("Access-Control-Allow-Origin: http://localhost:5173"); // 允许前端域名访问
header("Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS"); // 允许的请求方法
header("Access-Control-Allow-Headers: Content-Type, Authorization"); // 允许的请求头
header("Content-Type: application/json; charset=UTF-8"); // 返回 JSON 格式


// 初始化
$db = Database::getConnection();
$authController = new AuthController($db);
$postController = new PostController($db);
$topicController = new TopicController($db);
$categoryController = new CategoryController($db);
$userPostController = new UserPostController($db);

// 解析请求URI
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];

// 路由
if ($method === 'POST' && $url === '/login') {
	$authController->login();
} elseif ($method === 'POST' && $url === '/reg') {
	$authController->register();
} elseif ($method === 'GET' && $url === '/postLimit') {
	$postController->getTopPosts();
} elseif ($method === 'GET' && $url === '/postById') {
	$postController->getPostById();
} elseif ($method === 'GET' && $url === '/post/category') {
	$postController->getPostsByCategory();
} elseif ($method === 'GET' && $url === '/post/search') {
	$postController->searchPosts();
} elseif ($method === 'GET' && $url === '/post/comments') {
	$postController->getPostComments();
} elseif ($method === 'POST' && $url === '/post/commentInsert') {
	$postController->insertComment();
} elseif ($method === 'POST' && $url === '/post/release') {
	$postController->releasePost();
} elseif ($method === 'GET' && $url === '/topics') {
	$topicController->getLatestTopics();
} elseif ($method === 'GET' && $url === '/topics/category') {
	$topicController->getTopicsByCategory();
} elseif ($method === 'POST' && $url === '/topics/release') {
	$topicController->releaseTopic();
} elseif ($method === 'GET' && $url === '/postcategory') {
	$categoryController->getPostCategories();
} elseif ($method === 'GET' && $url === '/topicscategory') {
	$categoryController->getTopicsCategories();
} elseif ($method === 'GET' && $url === '/user/posts') {
	$userPostController->getUserPosts();
} elseif ($method === 'GET' && $url === '/user/topics') {
	$userPostController->getUserTopics();
} elseif ($method === 'DELETE' && $url === '/user/postdelete') {
	$userPostController->deleteUserPost();
} elseif ($method === 'DELETE' && $url === '/user/topicdelete') {
	$userPostController->deleteUserTopic();
} elseif ($method === 'OPTIONS') {
	// 处理预检请求
	http_response_code(200);
} else {
	http_response_code(404);
	echo json_encode(['error' => 'Not Found']);
}
?>